package com.qfedu.provider.util;

import io.minio.*;
import io.minio.http.Method;
import org.apache.tika.Tika;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

public class MyMinioUtil {
    private static final String MINIO_HOST="http://39.102.213.244:9000";
    private static final String MINIO_USER_ACCESS_KEY="zdsminio";
    private static final String MINIO_USER_SECRET_KEY="Admin123!";
    private static final String MINIO_BUCKET="bucket01";


    private static MinioClient minioClient;
    static {
        minioClient=MinioClient.builder().endpoint(MINIO_HOST).
                credentials(MINIO_USER_ACCESS_KEY,MINIO_USER_SECRET_KEY).build();
    }
    public static boolean createBucket(String bucketName){
        try {
            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public static List<String> queryBuckets(){
        ArrayList<String> nameList = new ArrayList<>();
        try {
            minioClient.listBuckets().forEach(b -> {
                nameList.add(b.name());
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nameList;
    }
    //minio里，资源称为对象
    public static String upload(String objName,byte[] data){
        Tika tika = new Tika();
        String contentType = tika.detect(data);
        System.out.println("识别的文件类型"+contentType);
        try {
            minioClient.putObject(PutObjectArgs.builder().bucket(MINIO_BUCKET)
                    .contentType(contentType)
                    .object(objName).stream(new ByteArrayInputStream(data), data.length, 0).build());
            return createUrl(objName);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }
    public static String createUrl(String objName) throws Exception{
        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
                .bucket(MINIO_BUCKET).object(objName).method(Method.GET).build());
    }
    //查询存储多少对象了
    public static List<String> queryObjectList(){
        ArrayList<String> objectList = new ArrayList<>();
        minioClient.listObjects(ListObjectsArgs.builder().bucket(MINIO_BUCKET)
                .build()).forEach(r->{
            try {
                objectList.add(r.get().objectName());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return objectList;
    }
    public static boolean deleteObject(String objName){
        try {
            minioClient.removeObject(RemoveObjectArgs.builder().bucket(MINIO_BUCKET).object(objName).build());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    public static boolean existObject(String objName){
        try {
            return minioClient.getObject(GetObjectArgs.builder().bucket(MINIO_BUCKET)
                    .object(objName).build())!=null;//是否不为空，不为空就是存在，为空就是不存在
        }catch (Exception e){

        }
        return false;
    }
}




//package com.qfedu.provider.util;
//
//import io.minio.*;
//import io.minio.http.Method;
//import org.apache.tika.Tika;
//
//import java.io.ByteArrayInputStream;
//import java.util.ArrayList;
//import java.util.List;
//
//public class MyMinioUtil {
//    private static final String MINIO_HOST="http://39.102.213.244:9000";
//    private static final String MINIO_USER_ACCESS_KEY="zdsminio";
//    private static final String MINIO_USER_SECRET_KEY="Admin123!";
//    private static final String MINIO_BUCKET="bucket01";
//    private static MinioClient minioClient;
//    static {
//        minioClient=MinioClient.builder().endpoint(MINIO_HOST).
//                credentials(MINIO_USER_ACCESS_KEY,MINIO_USER_SECRET_KEY).build();
//    }
//    public static boolean createBucket(String bucketName){
//        try {
//            if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
//                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
//                return true;
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return false;
//    }
//    public static List<String> queryBuckets(){
//        ArrayList<String> nameList = new ArrayList<>();
//        try {
//            minioClient.listBuckets().forEach(b -> {
//                nameList.add(b.name());
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return nameList;
//    }
//    //minio里，资源称为对象
//    public static String upload(String objName,byte[] data){
//        Tika tika = new Tika();
//        String contentType = tika.detect(data);
//        System.out.println("识别的文件类型"+contentType);
//        try {
//            minioClient.putObject(PutObjectArgs.builder().bucket(MINIO_BUCKET).contentType(contentType).object(objName).stream(new ByteArrayInputStream(data), data.length, 0).build());
//            return createUrl(objName);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return  null;
//    }
//    public static String createUrl(String objName) throws Exception{
//        return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()
//                .bucket(MINIO_BUCKET).object(objName).method(Method.GET).build());
//    }
//    //查询存储多少对象了
//    public static List<String> queryObjectList(){
//        ArrayList<String> objectList = new ArrayList<>();
//        minioClient.listObjects(ListObjectsArgs.builder().bucket(MINIO_BUCKET)
//                .build()).forEach(r->{
//            try {
//                objectList.add(r.get().objectName());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
//        return objectList;
//    }
//    public static boolean deleteObject(String objName){
//        try {
//            minioClient.removeObject(RemoveObjectArgs.builder().bucket(MINIO_BUCKET).object(objName).build());
//            return true;
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        return false;
//    }
//    public static boolean existObject(String objName){
//        try {
//            return minioClient.getObject(GetObjectArgs.builder().bucket(MINIO_BUCKET)
//                    .object(objName).build())!=null;//是否不为空，不为空就是存在，为空就是不存在
//        }catch (Exception e){
//
//        }
//        return false;
//    }
//}
